package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppStoreService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppStoreCreateValidate;
import com.mdd.admin.validate.yylq.TAppStoreSearchValidate;
import com.mdd.admin.validate.yylq.TAppStoreUpdateValidate;
import com.mdd.admin.vo.yylq.TAppStoreDetailVo;
import com.mdd.admin.vo.yylq.TAppStoreListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppStore;
import com.mdd.common.mapper.yangyangLeadingEnterprise.*;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 门店实现类
 * @author LikeAdmin
 */
@Service
public class TAppStoreServiceImpl implements ITAppStoreService {
        
    @Resource
    TAppStoreMapper tAppStoreMapper;

    @Resource
    TAppProvinceMapper appProvinceMapper;

    @Resource
    TAppCityMapper appCityMapper;

    @Resource
    TAppTownMapper appTownMapper;

    @Resource
    TAppDicMapper appDicMapper;

    /**
     * 门店列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppStoreListedVo>
     */
    @Override
    public PageResult<TAppStoreListedVo> list(PageValidate pageValidate, TAppStoreSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppStore> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tAppStoreMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:pic:str",
            "like:storeFullName@store_full_name:str",
            "=:longitude:long",
            "=:latitude:long",
            "=:businessStartTime@business_start_time:str",
            "=:businessEndTime@business_end_time:str",
            "=:phone:str",
            "=:provinceId@province_id:int",
            "=:cityId@city_id:int",
            "=:areaId@area_id:int",
            "=:address:str",
            "=:showAddress@show_address:str",
            "=:dicId@dic_id:int",
            "=:dicType@dic_type:str",
            "=:status:int",
        });

        IPage<TAppStore> iPage = tAppStoreMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppStoreListedVo> list = new LinkedList<>();
        for(TAppStore item : iPage.getRecords()) {
            TAppStoreListedVo vo = new TAppStoreListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.formatDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.formatDate(item.getUpdateTime()));
            vo.setBusinessStartTime(TimeUtils.formatDateForTime(item.getBusinessStartTime()));
            vo.setBusinessEndTime(TimeUtils.formatDateForTime(item.getBusinessEndTime()));
            if(item.getDicId()!=null){
                vo.setDicValue(appDicMapper.selectById(item.getDicId()).getTypeValue());
            }
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 门店详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppStore
     */
    @Override
    public TAppStoreDetailVo detail(Integer id) {
        TAppStore model = tAppStoreMapper.selectOne(
                new QueryWrapper<TAppStore>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppStoreDetailVo vo = new TAppStoreDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setBusinessStartTime(TimeUtils.formatDateForTime(model.getBusinessStartTime()));
        vo.setBusinessEndTime(TimeUtils.formatDateForTime(model.getBusinessEndTime()));
        return vo;
    }

    /**
     * 门店新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppStoreCreateValidate createValidate) {
        TAppStore model = new TAppStore();
        model.setName(createValidate.getName());
        model.setPic(createValidate.getPic());
        model.setStoreFullName(createValidate.getStoreFullName());
        model.setLongitude(createValidate.getLongitude());
        model.setLatitude(createValidate.getLatitude());
        model.setBusinessStartTime(createValidate.getBusinessStartTime());
        model.setBusinessEndTime(createValidate.getBusinessEndTime());
        model.setPhone(createValidate.getPhone());
        model.setProvinceName(createValidate.getProvinceName());
        model.setProvinceCode(createValidate.getProvinceCode());
        model.setCityName(createValidate.getCityName());
        model.setCityCode(createValidate.getCityCode());
        model.setAreaName(createValidate.getAreaName());
        model.setAreaCode(createValidate.getAreaCode());
        model.setAddress(createValidate.getAddress());
        model.setShowAddress(createValidate.getShowAddress());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setDicId(createValidate.getDicId());
        model.setDicType(createValidate.getDicType());
        model.setStatus(createValidate.getStatus());
        tAppStoreMapper.insert(model);
    }

    /**
     * 门店编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppStoreUpdateValidate updateValidate) {
        TAppStore model = tAppStoreMapper.selectOne(
                new QueryWrapper<TAppStore>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setPic(updateValidate.getPic());
        model.setStoreFullName(updateValidate.getStoreFullName());
        model.setLongitude(updateValidate.getLongitude());
        model.setLatitude(updateValidate.getLatitude());
        model.setBusinessStartTime(updateValidate.getBusinessStartTime());
        model.setBusinessEndTime(updateValidate.getBusinessEndTime());
        model.setPhone(updateValidate.getPhone());
        model.setProvinceName(updateValidate.getProvinceName());
        model.setProvinceCode(updateValidate.getProvinceCode());
        model.setCityName(updateValidate.getCityName());
        model.setCityCode(updateValidate.getCityCode());
        model.setAreaName(updateValidate.getAreaName());
        model.setAreaCode(updateValidate.getAreaCode());
        model.setAddress(updateValidate.getAddress());
        model.setShowAddress(updateValidate.getShowAddress());
        model.setUpdateTime(new Date());
        model.setDicId(updateValidate.getDicId());
        model.setDicType(updateValidate.getDicType());
        model.setStatus(updateValidate.getStatus());
        tAppStoreMapper.updateById(model);
    }

    /**
     * 门店删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppStore model = tAppStoreMapper.selectOne(
                new QueryWrapper<TAppStore>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppStoreMapper.delete(new QueryWrapper<TAppStore>().eq("id", id));
    }

}
